<template>
  <view class="container">
    <view class="product-recommend">
      <scroll-view class="product-scroll" scroll-x>
        <view 
          class="entry-item" 
          v-for="(item, index) in entries" 
          :key="index"
          @click="goToInfo(item.id)">  <!-- 添加点击事件 -->
          <image class="entry-img" :src="BASE_URL+item.imgUrl" mode="aspectFill"></image>
          <text class="entry-name">{{ item.categoryName }}</text>
        </view>
      </scroll-view>
    </view>
		
    <view class="product-recommend">
      <text class="recommend-title">产品推荐</text>
      <scroll-view class="product-scroll" scroll-x>
        <view 
          class="product-item" 
          v-for="(item, index) in products" 
          :key="index"
          @click="goToDetail(item.id)">  <!-- 添加点击事件 -->
          <image class="product-img" :src="BASE_URL+item.imgUrl" mode="aspectFill"></image>
          <text class="product-name">{{ item.proName }}</text>
        </view>
      </scroll-view>
    </view>

		    <!-- 修改后的服务入口 -->
		<view class="service-grid">
      <view 
        class="service-entry" 
        v-for="(item, index) in serviceEntries" 
        :key="index"
        @click="handleServiceClick(item)">  <!-- 添加点击事件 -->
        <view class="text-container">
          <view class="title-row">
            <text class="service-title">{{ item.title }}</text>
            <image class="title-icon" :src="item.icon" mode="aspectFit"></image>
          </view>
          <text class="service-desc">{{ item.desc }}</text>
        </view>
      </view>
    </view>

    <!-- 新增最新咨询模块 -->
    <view class="news-module">
      <text class="module-title">最新资讯</text>
      <scroll-view class="news-scroll" scroll-y>
        <view class="news-item" v-for="(item, index) in news" :key="index"  @click="goToNewsDetail(item.id)">
          <image class="news-image" :src="BASE_URL+item.imgUrl" mode="aspectFill"></image>
          <view class="text-content">
            <text class="news-title">{{ item.title }}</text>
            <text class="news-desc">{{ item.content }}</text>
          </view>
        </view>
      </scroll-view>
    </view>

    <!-- 原有产品推荐模块... -->
  </view>
</template>

<script>
import { getLastNewsPages, getNewsCategoryList } from '@/api/news'
import { saleProdList } from '@/api/product'
import {BASE_URL} from '@/utils/request'
export default {
  data() {
    return {
      BASE_URL: BASE_URL,
      entries: [],
      serviceEntries: [
        { 
          title: '联系我们',
          desc: '为您提供更好的产品和服务',
          icon: '/static/images/phone.png' 
        },
        { 
          title: '合作意向',
          desc: '搭建双方共赢的平台',
          icon: '/static/images/hand.png' 
        }
      ],
      products: [
        { id: 1, name: '产品一', image: '/static/logo.png' },
        { id: 2, name: '产品二', image: '/static/logo.png' },
        { id: 3, name: '产品三', image: '/static/logo.png' },
        { id: 4, name: '产品四', image: '/static/logo.png' }
      ],
      news: [
        {
          title: '行业新动态',
          content: '最新市场趋势分析报告发布...',
          image: '/static/news/1.jpg'
        },
        {
          title: '产品升级公告', 
          content: '全新版本功能优化说明...',
          image: '/static/news/2.jpg'
        },
        {
          title: '产品升级公告', 
          content: '全新版本功能优化说明...',
          image: '/static/news/2.jpg'
        }
      ]
    }
  },
  
  async created() {
      console.log('BASE_URL:', this.BASE_URL)
      try {
        // 获取新闻分类列表
        const res = await getNewsCategoryList(0)
        if(res){
          this.entries = res;
        }
      } catch (error) {
        console.error('获取分类列表失败', error)
      }
      try {
        // 获取最新新闻列表
        const res = await getLastNewsPages()
        if(res && res.list){
          res.list = res.list.map(item => {
            // 去除HTML标签并截取前100个字符
            item.content = item.content
              .replace(/<[^>]+>/g, '') // 去除HTML标签
              .substring(0, 100) // 截取前100个字符
              .trim(); // 去除首尾空格
            return item;
          });
          this.news = res.list;
        }
      } catch (error) {
        console.error('获取新闻列表失败', error)
      }
      try {
        // 获取推荐产品列表
        const res = await saleProdList(0)
        // console.log("res===",res);
        if(res){
          this.products = res;

        }
      } catch (error) {
        console.error('获取产品列表失败', error)
      }
      // 这里判断下token没失效可以跳过
      wx.login({
        success (res) {
          if (res.code) {
            //发起网络请求
            wx.request({
              url: this.BASE_URL+'/api/weixinLogin',
              data: {
                code: res.code
              },
              success (res) {
                if(res.data.code==="1"){
                  uni.setStorageSync('token',res.data.token)
                }else{
                  // console.log('微信登录失败！' + res.data.msgCode )
                }
              }
            })
          } else {
            // console.log('登录失败！' + res.data.msgCode+" "+res.data.msg )
          }
        }
      })
    },
  methods: {
    goToInfo(item) {
      // 通过全局事件传递参数
      // console.log("item===",item);
      uni.setStorageSync('tempInfoType', item);
      // 改用switchTab跳转
      uni.switchTab({
        url: '/pages/info/info'
      });
    },
    goToDetail(id) {
      uni.navigateTo({
        url: `/pages/product/detail?id=${id}`
      })
    },
    goToNewsDetail(id) {
      uni.navigateTo({
        url: `/pages/info/detail?id=${id}`
      })
    },
    handleServiceClick(item) {
      if (item.title === '合作意向') {
        uni.navigateTo({
          url: '/pages/contact/index'
        })
      } else if (item.title === '联系我们') {
        uni.openCustomerServicePage({
          sessionFrom: 'weapp',
          showMessageCard: true,
          sendMessageTitle: '在线客服',
          sendMessagePath: '/pages/index/index'
        })
      }
    }
  }
}
</script>

<style>
.container {
  padding: 20px;
  background: linear-gradient(to bottom, #000000, #eb0e0e); /* 科技蓝渐变背景 */
  min-height: 100vh;
}

.entry-grid {
  background: rgba(255, 255, 255, 0.9);
  border-radius: 12px;
  padding: 15px;
  display: flex;
  justify-content: space-between; /* 两端对齐 */
}

.entry-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: auto; /* 移除最小宽度限制 */
}

.icon-box {
  width: 48px;
  height: 48px;
  margin-bottom: 8px;
  /* 原有背景和圆角保持不变 */
}

.entry-label {
  font-size: 12px;
  color: #333;
  text-align: center;
}

.product-recommend {
  background: #ffffff;
  border-radius: 12px;
  padding: 15px;
  margin-top: 20px;
}

.recommend-title {
  font-size: 16px;
  color: #333;
  font-weight: bold;
  margin-bottom: 10px;
  display: block;
}

.product-scroll {
  width: 100%;
  white-space: nowrap;
}

.product-item {
  width: 30%;
  display: inline-block;
  margin-right: 10px;
}
.entry-item{
  width: 20%;
  display: inline-block;
  margin-right: 10px;
}

.product-img {
  width: 80px;
  height: 80px;
  border-radius: 8px;
  background: #f5f5f5;
}
.entry-img{
  width: 50px;
  height: 50px;
  border-radius: 8px;
  background: #f5f5f5;
}

.product-name {
  font-size: 12px;
  color: #666;
  text-align: center;
  margin-top: 5px;
  display: block;
}

.entry-name {
  font-size: 12px;
  color: #666;
  text-align: center;
  margin-top: 5px;
  display: block;
}

/* 修改后的服务入口样式 */
.service-grid {
  background: transparent;
  margin-top: 15px;
}

.service-entry {
  background: #e3f2fd;
  border-radius: 12px;
  padding: 15px;
  margin-bottom: 10px;
  position: relative;
}

.title-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.title-icon {
  width: 20px;
  height: 20px;
}

.service-title {
  font-size: 14px;
  color: #1a237e;
  font-weight: bold;
}

.service-entry:last-child {
  margin-bottom: 0;
}

.text-container {
  flex: 1;
  margin-left: 15px;
}

.service-entry {
  flex: 1;
  background: #e3f2fd;
  border-radius: 12px;
  padding: 15px 35px 15px 15px;
  position: relative;
}

.service-icon {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 20px;
  height: 20px;
}

.service-title {
  font-size: 14px;
  color: #1a237e;
  font-weight: bold;
  margin-bottom: 4px;
}

.service-desc {
  font-size: 12px;
  color: #666;
  line-height: 1.4;
}

/* 在服务入口样式后添加 */
.news-module {
  background: #fff;
  border-radius: 12px;
  padding: 15px;
  margin-top: 15px;
  height: 300px;
}

.news-scroll {
  height: calc(100% - 30px);
}

.news-item {
  display: flex;
  gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid #eee;
}

.news-image {
  width: 100px;
  height: 80px;
  border-radius: 6px;
  flex-shrink: 0;
}

.text-content {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.news-title {
  font-size: 14px;
  color: #333;
  font-weight: 500;
  margin-bottom: 8px;
}

.news-desc {
  font-size: 12px;
  color: #666;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
}
</style>
